'''
Created on Jul 9, 2011

@author: kafkef
'''

from PyQt4.QtGui import *
from PyQt4.QtCore import QObject, SIGNAL, SLOT, pyqtSlot, Qt


from dexen.system.client import ui
from dexen.system.client.proxy import JobProxy
from dexen.system.client.ui.job_page import JobTabWidget
from dexen.system.client.ui.job_navigator import JobNavigatorWidget



svr_proxy = ui.svr_proxy

class JobWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self._create_widgets()
        #self.job_proxies = {} # job_id:JobProxy
    
    def _create_widgets(self):
        #create widgets
        self.navigator = JobNavigatorWidget()
        self.tab_widget = JobTabWidget()

        #setup layout
        layout = QHBoxLayout()
        splitter = QSplitter(self)
        
        splitter.addWidget(self.navigator)
        splitter.addWidget(self.tab_widget)
        layout.addWidget(splitter)
        
        self.setLayout(layout)

        #connect signal/slots        
        QObject.connect(self.navigator, SIGNAL("on_refresh"),
                        self.on_job_refresh)
        QObject.connect(self.navigator, SIGNAL("cur_job_changed"), 
                        self.on_cur_job_changed)
    
    def on_server_connected(self):
        self.navigator.on_server_connected()
        #self.navigator.hide()
    
    def on_job_refresh(self):
        self.setCursor(QCursor(Qt.WaitCursor))
        print "JobWidget:on_job_refresh()"
        self.tab_widget.on_refresh()
        self.setCursor(QCursor(Qt.ArrowCursor))
    
    def on_cur_job_changed(self, job_id):
        self.setCursor(QCursor(Qt.WaitCursor))
        print "changed to job", job_id
        self.tab_widget.on_cur_job_changed(job_id)
        self.setCursor(QCursor(Qt.ArrowCursor))
        
        """
        if not self.job_proxies.has_key(job_id):
            self.job_proxies[job_id] = svr_proxy.get_job(job_id)
        job_proxy = self.job_proxies[job_id]
        assert isinstance(job_proxy, JobProxy)
        job_proxy.refresh()
        """
